package com.century.sjt.window;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.Pay.MerchantActivity;

/**
 * Created by 炳基 on 2016/7/28.
 */
public class ChooseMobilePopupWindow extends PopupWindow{
    private Context mContext;
    private Button btn_Dismiss;
    private View mMenuView;
    private RequestQueue mQueue;
    private LinearLayout layout_Mobile1,layout_Mobile2;
    private TextView tvMobile1,tvMobile2;
    private View view_1,view_2;

    public ChooseMobilePopupWindow(final Activity context, final String Mobile1,final String Mobile2) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.sjt_choosemobile_window, null);
        mQueue = Volley.newRequestQueue(mMenuView.getContext());
        btn_Dismiss = (Button) mMenuView.findViewById(R.id.btn_dismiss);
        layout_Mobile1 = (LinearLayout) mMenuView.findViewById(R.id.layout_phone1);
        layout_Mobile2 = (LinearLayout) mMenuView.findViewById(R.id.layout_phone2);
        tvMobile1 = (TextView) mMenuView.findViewById(R.id.tv_phome1);
        tvMobile2 = (TextView) mMenuView.findViewById(R.id.tv_phome2);
        view_1 = (View) mMenuView.findViewById(R.id.view_1);
        view_2 = (View) mMenuView.findViewById(R.id.view_2);
        //设置按钮监听

        //设置PayPopupWindow的View
        this.setContentView(mMenuView);
        //设置PayPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置PayPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PayPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PayPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置PayPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
        if ("".equals(Mobile1)||null==Mobile1){
            layout_Mobile1.setVisibility(View.GONE);
            view_1.setVisibility(View.GONE);
        }else {
            tvMobile1.setText(Mobile1);
        }
        if ("".equals(Mobile2)||null==Mobile2){
            layout_Mobile2.setVisibility(View.GONE);
            view_2.setVisibility(View.GONE);
        }else {
            tvMobile2.setText(Mobile2);
        }
        layout_Mobile1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Mobile1));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
        layout_Mobile2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Mobile2));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
        btn_Dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }

    public void showAppayRes(final String res) {
        new AlertDialog.Builder(mContext)
                .setMessage(res)
                .setTitle(mContext.getResources().getString(R.string.text_prompt))
                .setPositiveButton(mContext.getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("payType", "2");
                        intent.setClass(mContext, MerchantActivity.class);
                        mContext.startActivity(intent);

                    }
                })
                .show();
    }

}
